package com.prs.crm.action.online;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.online.LeadEmail;
import com.prs.crm.domain.online.LeadEmailSee;
import com.prs.crm.service.online.LeadEmailSeeService;
import com.prs.crm.service.online.LeadEmailService;

public class LeadEmailAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private LeadEmail leadEmail;
	private LeadEmailService leadEmailService;
	private String la;
	private LeadEmailSeeService leadEmailSeeService;
	private LeadEmailSee leadEmailSee;
	private String serCode;
	public String getSerCode() {
		return serCode;
	}
	public void setSerCode(String serCode) {
		this.serCode = serCode;
	}
	public LeadEmailSee getLeadEmailSee() {
		return leadEmailSee;
	}
	public void setLeadEmailSee(LeadEmailSee leadEmailSee) {
		this.leadEmailSee = leadEmailSee;
	}
	public LeadEmailSeeService getLeadEmailSeeService() {
		return leadEmailSeeService;
	}
	public void setLeadEmailSeeService(LeadEmailSeeService leadEmailSeeService) {
		this.leadEmailSeeService = leadEmailSeeService;
	}
	public String getLa() {
		return la;
	}
	public void setLa(String la) {
		this.la = la;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public LeadEmail getLeadEmail() {
		return leadEmail;
	}
	public void setLeadEmail(LeadEmail leadEmail) {
		this.leadEmail = leadEmail;
	}
	public LeadEmailService getLeadEmailService() {
		return leadEmailService;
	}
	public void setLeadEmailService(LeadEmailService leadEmailService) {
		this.leadEmailService = leadEmailService;
	}
	
	@Action("listLeadEmail")
	public String listLeadEmail(){
		List<LeadEmail> email=this.getLeadEmailService().getAllLeadEmail();
		for (LeadEmail leadEmail : email) {
//			List<LeadEmailSee> le=this.getLeadEmailSeeService().getLeadEmailSees(leadEmail.getLeadEmailSeeId());
//			for (LeadEmailSee leadEmailSee : le) {
//				String[] personid=leadEmailSee.getPersonId().split(",");
//				for (String pid : personid) {
//					System.out.println(pid);
//					if ((AppContext.getAccountContext().currentUser().getId()+"").equals(pid)) {
//						leadEmail.setShowState(AppContext.getAccountContext().currentUser().getId()+":1");
//						this.getLeadEmailService().submit(leadEmail);
//						break;
//					}else{
//						leadEmail.setShowState("other");
//						this.getLeadEmailService().submit(leadEmail);
//					}
//				}
//			}
			String[] personid=leadEmail.getShowPersonId().split(",");
			for (String pid : personid) {
				if ((AppContext.getAccountContext().currentUser().getId()+"").equals(pid)) {
					leadEmail.setShowState(AppContext.getAccountContext().currentUser().getId()+":1");
					this.getLeadEmailService().submit(leadEmail);
					break;
				}else{
					leadEmail.setShowState("other");
					this.getLeadEmailService().submit(leadEmail);
				}
			}
		}
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(LeadEmail.class);
		criteria.add(Restrictions.eq("showState", AppContext.getAccountContext().currentUser().getId()+":1"));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getLeadEmailService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("listLeadEmailManager")
	public String listLeadEmailManager(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(LeadEmail.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getLeadEmailService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("editLeadEmail")
	public String edit(){
		return SUCCESS;
	}
	
	@Action("viewLeadEmail")
	public String viewLeadEmail(){
		if (ids!=null) {
			this.leadEmail=this.getLeadEmailService().get(ids[0]);
		}
		if(leadEmail.getType()!=null){
			la="1";
		}else{
			la="0";
		}
		return SUCCESS;
	}
	
	@Action(value="saveAutonym",results={
			@Result(name="success",type="redirect",location="editLeadEmail"),
			@Result(name ="error",type="chain",location="editLeadEmail")
	})
	public String saveAutonym(){
		LeadEmailSee le=this.getLeadEmailSeeService().getLeadEmailSee("ld01");
		if (le==null) {
			this.addActionError("还没设置信件可见人员，请到信箱管理里面去设置！");
			return ERROR;
		}else{
			if ("".equals(le.getPersonId())) {
				this.addActionError("还没设置信件可见人员，请到信箱管理里面去设置！");
				return ERROR;
			}
			leadEmail.setLeadEmailSeeId(le.getId());
			leadEmail.setShowPersonId(le.getPersonId());
			leadEmail.setType("");
			this.getLeadEmailService().save(leadEmail);
			leadEmail.setPerson(leadEmail.getRecorder().getResume().getName());
			this.getLeadEmailService().save(leadEmail);
		}
		return SUCCESS;
	}
	
	@Action(value="saveAnonymity",results={
			@Result(name="success",type="redirect",location="editLeadEmail"),
			@Result(name="error",type="chain",location="editLeadEmail")
	})
	public String saveAnonymity(){
		LeadEmailSee le=this.getLeadEmailSeeService().getLeadEmailSee("ld01");
		if (le==null) {
			this.addActionError("还没设置信件可见人员，请到信箱管理里面去设置！");
			return ERROR;
		}else{
			if ("".equals(le.getPersonId())) {
				this.addActionError("还没设置信件可见人员，请到信箱管理里面去设置！");
				return ERROR;
			}
		leadEmail.setLeadEmailSeeId(le.getId());
		leadEmail.setShowPersonId(le.getPersonId());
		this.getLeadEmailService().save(leadEmail);
		leadEmail.setPerson(leadEmail.getRecorder().getResume().getName());
		this.getLeadEmailService().save(leadEmail);
		}
		return SUCCESS;
	}
	
	@Action(value="removeLeadEmail",results={
			@Result(name="success",type="redirect",location="listLeadEmailManager"),
			@Result(name="error",type="chain",location="listLeadEmailManager")
	})
	public String removeLeadEmail(){
		if (ids!=null) {
			this.leadEmail=this.getLeadEmailService().get(ids[0]);
		}
		this.getLeadEmailService().batchRemove(ids);
		return SUCCESS;
	} 
	
	@Action("editLeadMailSee")
	public String editLeadMailSee(){
		this.leadEmailSee=this.getLeadEmailSeeService().getLeadEmailSee(serCode);
		return SUCCESS;
	}
	@Action(value="saveLeadMailSee",results={
			@Result(name="success",type="redirect",location="listLeadEmailManager"),
			@Result(name="error",type="chain",location="editLeadMailSee?serCode=${leadEmailSee.getType()}")
	})
	public String saveLeadMailSee(){
		LeadEmailSee le=this.getLeadEmailSeeService().getLeadEmailSee("ld01");
		if (le==null) {
			le=new LeadEmailSee();
			le.setType("ld01");
			le.setPersonName(leadEmailSee.getPersonName());
			le.setPersonId(leadEmailSee.getPersonId());
			this.getLeadEmailSeeService().save(le);
		}else{
			le.setPersonName(leadEmailSee.getPersonName());
			le.setPersonId(leadEmailSee.getPersonId());
			this.getLeadEmailSeeService().submit(le);
		}
		leadEmailSee=le;
		return SUCCESS;
	}
}
